from ..models.role_md import RoleMD
from ..models.user_account_md import UserAccountMD
from flask_app.extensions import login_manager


class UserAdminSVR():
    def __init__(self,login_name):
        self.ua = self.load_user(login_name)

    @staticmethod
    def register(login_name,pass_word):
        ua = UserAccountMD(login_name,pass_word,{})
        ua.save()

    @login_manager.user_loader
    @staticmethod
    def load_user(login_name):
        ua = UserAccountMD.get_user_account(login_name)
        return ua

    @staticmethod
    def delete_role(role_id, force_del=0):
        role_md = RoleMD.get(role_id)
        status,msg = role_md.delete(force_del)
        return status,msg

    @staticmethod
    def get_role(role_id):
        return RoleMD.get(role_id)

    @staticmethod
    def create_role(role_name,role_type,parent_id):
        role_md=RoleMD(role_name=role_name, role_type=role_type,parent_id=parent_id)
        RoleMD.add(role_md)
        return role_md

    @staticmethod
    def update_role(role_id, role_name, role_type, role_parent_id):
        role_md = RoleMD.get(role_id)
        role_md.name = role_name
        role_md.type = role_type
        role_md.parent_id = role_parent_id
        role_md.update()



    @staticmethod
    def check_author(controller_fcn):
        pass

    @staticmethod
    def get_all_roles():
        return [role for role in RoleMD.query.all()]

    @staticmethod
    def get_role(role_id):
        return RoleMD(role_id)








